#SHELL FILE FOR CLEANING RDW DATA IN R
#PLEASE REPLACE THE WORKING DIRECTORY WITH THE ONE WHERE THE FILES ARE LOCATED


rm(list=ls()); gc()
objects()

#Set working directory (CHANGE WITH DIRECTORY WHERE THE FILES ARE LOCATED)
setwd("J:/Projects/Recall project/Replication folder")

#install.packages("groundhog")
library("groundhog") #Allows reproducibility by allowing loading of package versions as of a specific date

package.date<-"2021-10-01"
groundhog.library("lubridate", package.date) #Easy date modification
'OK' #Allows Groundhog to save chosen version of packages (without deleting existing ones)
groundhog.library("stringr", package.date)  #Easy string reading function
groundhog.library("plyr", package.date) #Rbind dataframes with some columns missing
groundhog.library("ggplot2", package.date) #Graphs
groundhog.library("lfe", package.date) #Two-way fixed effects
groundhog.library("gridExtra", package.date) #Arrange graphs together
groundhog.library("ggrepel", package.date) #Non-overlapping labels in plot
groundhog.library("Rcpp", package.date) #Forggrepel
groundhog.library("xtable", package.date) #Latex table
groundhog.library("data.table", package.date) #Latex table
groundhog.library("papeR", package.date) #Summary stats
groundhog.library("gtools", package.date)





#CLEAN REGISTRATIONS, GET MONTH OF SCRAPPAGE, RESELL DATES AND EXPORT DATES
source("1_Clean_registrations.R", echo=TRUE, max.deparse.length=10000000)

#CLEAN RECALLS AND MERGE WITH REGISTRATIONS
rm(list=ls()); gc()
objects()
source("2_Clean_recalls_2019.R", echo=TRUE, max.deparse.length=10000000)

#GENERATE COMPACT RECALL STATUS FILE
rm(list=ls()); gc()
objects()
source("3_Clean_recall_status.R", echo=TRUE, max.deparse.length=10000000)

#RECOVER RECALL STATUS INFORMATION
rm(list=ls()); gc()
objects()
source("4_Complete_Recalls.R", echo=TRUE, max.deparse.length=10000000)

#CREATE BASE PANEL
rm(list=ls()); gc()
objects()
source("5_Create_Panel_Full.R", echo=TRUE, max.deparse.length=10000000)

#CREATE LINK WITH CAR CHARACTERISTICS
rm(list=ls()); gc()
objects()
source("6_Create_Link_Char.R", echo=TRUE, max.deparse.length=10000000)

#CREATE LINK WITH BRAND/MANUFACTURER NAMES
rm(list=ls()); gc()
objects()
source("7_Create_Link_Make.R", echo=TRUE, max.deparse.length=10000000)

#CLEAN APK INSPECTION DATA
rm(list=ls()); gc()
objects()
source("8_Clean_inspection.R", echo=TRUE, max.deparse.length=10000000)

#CREATE LINK WITH RECALL DATE
rm(list=ls()); gc()
objects()
source("9_Create_Link_Date.R", echo=TRUE, max.deparse.length=10000000)

#CREATE FINAL PANEL FILE FOR MAIN ANALYSIS
rm(list=ls()); gc()
objects()
source("10_Complete_Panel.R", echo=TRUE, max.deparse.length=10000000)

#CREATE PANEL FOR DIFFERENCE IN DIFFERENCES GRAPH
rm(list=ls()); gc()
objects()
source("11_Create_Panel_Ver.R", echo=TRUE, max.deparse.length=10000000)

#GET INFORMATION ON RECALL CHARACTERISTICS
rm(list=ls()); gc()
objects()
source("12_Recall_info_2019.R", echo=TRUE, max.deparse.length=10000000)

#CREATE PANEL FOR DIFFERENCE IN DIFFERENCES GRAPH FOR APPENDIX 
rm(list=ls()); gc()
objects()
source("13_Create_Panel_Variant.R", echo=TRUE, max.deparse.length=10000000)

#CREATE PANEL FOR DIFFERENCE IN DIFFERENCES GRAPH FOR APPENDIX 
rm(list=ls()); gc()
objects()
source("14_Create_Panel_Model.R", echo=TRUE, max.deparse.length=10000000)

